package AS3 {
import flash.display.DisplayObject;
import flash.profiler.profile;

import flash.sampler._getInvocationCount;

import mx.containers.Canvas;
import mx.controls.Alert;
import mx.controls.Button;
import mx.controls.Image;
import mx.controls.Label;
import mx.controls.RadioButton;
import mx.controls.Text;
import mx.controls.TextArea;
import mx.core.UIComponent;
import mx.utils.StringUtil;

import mx.utils.object_proxy;


import zakladki.QuizToolTip;
import zakladki.QuizPanel;


public class QuizProcessor {

    private var activeBookmark:int = 1;
    private var activeQuestion:int = 1;

    private var maxQuestionNumber:int = 4;
    private var tooltipAreaInitialY:int;

    private var CORRECT_ANSWER_SCORE:int = 10;
    private var WRONG_ANSWER_SCORE:int = 1;

    private var answerResponseText:String;
    private var answer:int = -1;
    private var correctAnswer:int = -2;
    private var correctAnswerAlt:int = -2;

    private var questionAnswered:Boolean = false;
    private var correctAnswerClicked:Boolean = false;

    private var quizXML:XML;
    private var btnHelpSkip:Button;
    private var lblHelp1:Text;
    private var cnvHelpSmall:Canvas;
    private var dimQuizImg:Image;

    private var quiz_radio1:RadioButton;
    private var quiz_radio2:RadioButton;
    private var quiz_radio3:RadioButton;
    private var quiz_radio4:RadioButton;
    private var quiz_radio5:RadioButton;

    private var quiz_radio1_label:Text;
    private var quiz_radio2_label:Text;
    private var quiz_radio3_label:Text;
    private var quiz_radio4_label:Text;
    private var quiz_radio5_label:Text;

    private var quizToolTip:QuizToolTip;

    private var mainProcessor:MainProcessor;

    private var lang:String;

    private var rebBulbSrc:String = "assets/img/quiz/red-bulb.png";
    private var yellowBulbSrc:String = "assets/img/quiz/yellow-bulb.png";

    private var parentUI:QuizPanel;
    private var lineTextHeight:Number;
    private var questionLineTextHeight:Number;

    private var bulbsPerRow:int;

    public function QuizProcessor(lang:String,parent:QuizPanel):void{
        this.lang = lang;
        this.parentUI = parent;
        tooltipAreaInitialY = getScaledConstYPosition(50);
        lineTextHeight = 0.05 * parentUI.height;
        questionLineTextHeight = 1.1 * lineTextHeight;

        bulbsPerRow = parent.bulbsContainer.width/32 - 1;

    }

    public function clickAnswered(answerNumber:int):void {
        var quizToolTipY:Number;

        correctAnswer = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).wlasciwa;
        correctAnswerAlt = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).wlasciwa_alt;
        if (answerNumber == 1) {
            answerResponseText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).odp_a;
            quizToolTipY = quiz_radio1.y;
        }
        if (answerNumber == 2) {
            answerResponseText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).odp_b;
            quizToolTipY = quiz_radio2.y;
        }
        if (answerNumber == 3) {
            answerResponseText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).odp_c;
            quizToolTipY = quiz_radio3.y;
        }
        if (answerNumber == 4) {
            answerResponseText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).odp_d;
            quizToolTipY = quiz_radio4.y;
        }
        if (answerNumber >= 5) {
            answerResponseText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).odp_e;
            quizToolTipY = quiz_radio5.y;
        }

        answer = answerNumber;
        var correct:Boolean = false;

        if (!correctAnswerClicked) {
            if (correctAnswerAlt == answer || correctAnswer == 0 || correctAnswer == answer) {
                mainProcessor.updateUserScore(CORRECT_ANSWER_SCORE, 1, 0);
                correct = true;
            }
            else {
                mainProcessor.updateUserScore(WRONG_ANSWER_SCORE, 0, 1);
            }
        }

        quizToolTip.height = calcHeight(answerResponseText);

        if (!questionAnswered) {
            addImage(parentUI.bulbsContainer, correct);
        }

        if (!correctAnswerClicked) {
            parentUI.enableNextButton(correct);
        }

        quizToolTip.y = quizToolTipY + parentUI.y;
        quizToolTip.x = parentUI.x + parentUI.width * 0.05;
        quizToolTip.setAnswer(answerResponseText);
        quizToolTip.qzImg.height = quizToolTip.height;

        quizToolTip.visible = true;

        questionAnswered = true;
        if (!correctAnswerClicked) {
            correctAnswerClicked = correct;
        }
    }

    private function addImage(canvas:Canvas,correctAnswer:Boolean):void {
        var imgYOffset:Number = canvas.height * 0.1;
        var childNum:int =  canvas.getChildren().length;
        var currRow:int = Math.ceil(childNum/bulbsPerRow);
        if(currRow == 0){
           currRow = 1;
        }
        var nextRow:int = Math.ceil((childNum + 1)/bulbsPerRow);
        var lastChild:DisplayObject = childNum > 0 ? canvas.getChildAt(childNum - 1) : null;

        var image:Image = new Image();
        if(correctAnswer){
            image.source = yellowBulbSrc;
        }
        else{
            image.source = rebBulbSrc;
        }

        if(lastChild == null || nextRow > currRow){
           image.x = 0;
           image.y = imgYOffset + (nextRow - 1) * 32;
        }
        else if(lastChild != null){
           image.x = lastChild.x + lastChild.width;
           image.y = lastChild.y;
        }

        image.visible = true;


        canvas.addChild(image);

    }

    public function quiz(bookmarkNumber:int):void {
        activeBookmark = bookmarkNumber;

        activeQuestion = 1;
        maxQuestionNumber = quizXML.zakladka.(numer == activeBookmark).max_pyt;

        resetQuiz();
        setViewForNextQuestion();
    }

    public function nextQuestion():void {
        activeQuestion++;
        resetQuiz();
        setViewForNextQuestion();
    }

    public function hasMoreQuestions():Boolean {
        return activeQuestion + 1 <= maxQuestionNumber;
    }

    private function getLastVisible():RadioButton {
        if(quiz_radio5.visible){
           return quiz_radio5;
        }
        if(quiz_radio4.visible){
           return quiz_radio4;
        }
        if(quiz_radio3.visible){
           return quiz_radio3;
        }
        if(quiz_radio2.visible){
           return quiz_radio2;
        }
        return quiz_radio1;
    }

    private function getScaledConstYPosition(position:Number):Number {
        return position/536 * parentUI.height;
    }

    private function setViewForNextQuestion():void {
        tooltipAreaInitialY = getScaledConstYPosition(50);
        maxQuestionNumber = quizXML.zakladka.(numer == activeBookmark).max_pyt;

        if(activeQuestion <= maxQuestionNumber){
            var questionText:String = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).pyt_tekst;
            var enterCounts:int = countEnters(questionText);

            lblHelp1.htmlText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).pyt_tekst;
            lblHelp1.validateNow();

            tooltipAreaInitialY = lblHelp1.y  + lblHelp1.textHeight + 0.05 * parentUI.height;
            setPossibleAnswers();

            var lastRadio:RadioButton = getLastVisible();

            parentUI.bulbsContainer.y = lastRadio.y + lastRadio.height + 0.02 * parentUI.height;
            parentUI.bulbsContainer.height = 0.9 * parentUI.height - parentUI.bulbsContainer.y;
            parentUI.validateNow();
        }

        dimQuizImg.visible = true;
        cnvHelpSmall.visible = true;
    }

    private function resetQuiz():void {
        answer = -1;
        correctAnswer = -2;
        correctAnswerAlt = -2;
        questionAnswered = false;
        correctAnswerClicked = false;

        quizToolTip.visible = false;

        if (quiz_radio1.selected) {
            quiz_radio1.selected = false;
        }
        if (quiz_radio2.selected) {
            quiz_radio2.selected = false;
        }
        if (quiz_radio3.selected) {
            quiz_radio3.selected = false;
        }
        if (quiz_radio4.selected) {
            quiz_radio4.selected = false;
        }
        if (quiz_radio5.selected) {
            quiz_radio5.selected = false;
        }

    }

    public function skipQuiz():void {
        resetQuiz();
        dimQuizImg.visible = false;
        cnvHelpSmall.visible = false;
    }

    private function setPossibleAnswers():void {
        var str:String = new String("");
        var enterCounts:int = 0;
        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).a != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).a;
            enterCounts = countEnters(str);
            quiz_radio1_label.y = tooltipAreaInitialY;
            quiz_radio1_label.height = 1.5 *lineTextHeight + enterCounts * lineTextHeight;
            quiz_radio1.y = tooltipAreaInitialY + enterCounts * lineTextHeight/2;
            tooltipAreaInitialY = tooltipAreaInitialY + 1.5 * lineTextHeight + enterCounts * lineTextHeight;
            quiz_radio1_label.htmlText = str;
            quiz_radio1.visible = true;
            quiz_radio1_label.visible = true;
        }
        else {
            quiz_radio1.visible = false;
            quiz_radio1_label.visible = false;
        }

        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).b != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).b;
            enterCounts = countEnters(str);
            quiz_radio2_label.y = tooltipAreaInitialY;
            quiz_radio2_label.height = 1.5 *lineTextHeight + enterCounts * lineTextHeight;
            quiz_radio2.y = tooltipAreaInitialY + enterCounts * lineTextHeight/2;
            tooltipAreaInitialY = tooltipAreaInitialY + 1.5 * lineTextHeight + enterCounts * lineTextHeight;
            quiz_radio2_label.htmlText = str;
            quiz_radio2.visible = true;
            quiz_radio2_label.visible = true;
        }
        else {
            quiz_radio2.visible = false;
            quiz_radio2_label.visible = false;
        }

        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).c != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).c;
            enterCounts = countEnters(str);
            quiz_radio3_label.y = tooltipAreaInitialY;
            quiz_radio3_label.height = 1.5 *lineTextHeight + enterCounts * lineTextHeight;
            quiz_radio3.y = tooltipAreaInitialY + enterCounts * lineTextHeight/2;
            tooltipAreaInitialY = tooltipAreaInitialY + 1.5 * lineTextHeight + enterCounts * lineTextHeight;
            quiz_radio3_label.htmlText = str;
            quiz_radio3.visible = true;
            quiz_radio3_label.visible = true;
        }
        else {
            quiz_radio3.visible = false;
            quiz_radio3_label.visible = false;
        }

        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).d != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).d;
            enterCounts = countEnters(str);
            quiz_radio4_label.y = tooltipAreaInitialY;
            quiz_radio4_label.height = 1.5 *lineTextHeight + enterCounts * lineTextHeight;
            quiz_radio4.y = tooltipAreaInitialY + enterCounts * lineTextHeight/2;
            tooltipAreaInitialY = tooltipAreaInitialY + 1.5 * lineTextHeight + enterCounts * lineTextHeight;
            quiz_radio4_label.htmlText = str;
            quiz_radio4.visible = true;
            quiz_radio4_label.visible = true;
        }
        else {
            quiz_radio4.visible = false;
            quiz_radio4_label.visible = false;
        }

        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).e != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).e;
            enterCounts = countEnters(str);
            quiz_radio5_label.y = tooltipAreaInitialY;
            quiz_radio5_label.height = 1.5 *lineTextHeight +  enterCounts * lineTextHeight;
            quiz_radio5.y = tooltipAreaInitialY + enterCounts * lineTextHeight/2;
            tooltipAreaInitialY = tooltipAreaInitialY + 1.5 * lineTextHeight + enterCounts * lineTextHeight;
            quiz_radio5_label.htmlText = str;
            quiz_radio5.visible = true;
            quiz_radio5_label.visible = true;
        }
        else {
            quiz_radio5.visible = false;
            quiz_radio5_label.visible = false;
        }

        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).f != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).f;
            //entery = liczEntery(str);
            //quiz_radio6_label.y = podstawa;
            //quiz_radio6_label.height = 20 + entery * 15;
            //quiz_radio6.y = podstawa + entery * 7.5;
            //podstawa = podstawa + 34 + entery * 15;
            //quiz_radio6_label.htmlText = str;
            //quiz_radio6.visible=true;quiz_radio6_label.visible=true;
        }
        //else {quiz_radio6.visible=false;quiz_radio6_label.visible=false;}
        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).g != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).g;
            //entery = liczEntery(str);
            //quiz_radio7_label.y = podstawa;
            //quiz_radio7_label.height = 20 + entery * 15;
            //quiz_radio7.y = podstawa + entery * 7.5;
            //podstawa = podstawa + 34 + entery * 15;
            //quiz_radio7_label.htmlText = str;
            //quiz_radio7.visible=true;quiz_radio7_label.visible=true;
        }
        //else {quiz_radio7.visible=false;quiz_radio7_label.visible=false;}
        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).h != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).h;
            //entery = liczEntery(str);
            //quiz_radio8_label.y = podstawa;
            //quiz_radio8_label.height = 20 + entery * 15;
            //quiz_radio8.y = podstawa + entery * 7.5;
            //podstawa = podstawa + 34 + entery * 15;
            //quiz_radio8_label.htmlText = str;
            //quiz_radio8.visible=true;quiz_radio8_label.visible=true;
        }
        //else {quiz_radio8.visible=false;quiz_radio8_label.visible=false;}
    }

    private function countEnters(tekst:String):int {
        return tekst.length / 85;
    }

    private function calcHeight(txt:String):Number {
        var enterCnt:int = txt.length/45;

        if(enterCnt < 5){
           return 150;
        }

        return 150 + ((int)((enterCnt - 5)/3 + 1)) * 55;
    }

    public function setQuizyXML(value:XML):void {
        quizXML = value;
    }

    public function setButtons(btnHelpDalej:Button):void {
        this.btnHelpSkip = btnHelpDalej;
    }

    public function setHelpLabel(label:Text):void {
        this.lblHelp1 = label;
    }

    public function setDarknessImg(image:Image):void {
        this.dimQuizImg = image;
    }

    public function setCanvas(canvas:Canvas):void {
        this.cnvHelpSmall = canvas;
    }

    public function setQuizToolTip(quizToolTip:QuizToolTip):void {
        this.quizToolTip = quizToolTip;
    }

    public function setButtonRadios(radio1:RadioButton, radio2:RadioButton, radio3:RadioButton, radio4:RadioButton, radio5:RadioButton):void {
        this.quiz_radio1 = radio1;
        this.quiz_radio2 = radio2;
        this.quiz_radio3 = radio3;
        this.quiz_radio4 = radio4;
        this.quiz_radio5 = radio5;
    }

    public function setButtonRadiosLabels(label1:Text, label2:Text, label3:Text, label4:Text, label5:Text):void {
        this.quiz_radio1_label = label1;
        this.quiz_radio2_label = label2;
        this.quiz_radio3_label = label3;
        this.quiz_radio4_label = label4;
        this.quiz_radio5_label = label5;
    }

    private function enableRadios(enabled:Boolean):void{
        quiz_radio1.enabled = enabled;
        quiz_radio2.enabled = enabled;
        quiz_radio3.enabled = enabled;
        quiz_radio4.enabled = enabled;
        quiz_radio5.enabled = enabled;
    }

    private function setRadiosVisibility(visible:Boolean):void {
        quiz_radio1.visible = visible;
        quiz_radio2.visible = visible;
        quiz_radio3.visible = visible;
        quiz_radio4.visible = visible;
        quiz_radio5.visible = visible;
    }

    private function setRadioLabelsVisibility(visible:Boolean):void {
        quiz_radio1_label.visible = visible;
        quiz_radio2_label.visible = visible;
        quiz_radio3_label.visible = visible;
        quiz_radio4_label.visible = visible;
        quiz_radio5_label.visible = visible;
    }





    public function setMainProcessor(mainProcessor:MainProcessor):void {
        this.mainProcessor = mainProcessor;
    }

}
}

